var g_curren_node;

//显示combobox的选项值
var taskTypeList = [
    {typeCode:'log',type:'stream',typeName:'日志流水'},
    {typeCode:'request',type:'stream',typeName:'请求流水'},
    {typeCode:'business',type:'stream',typeName:'业务流水'},
    {typeCode:'setFile',type:'file',typeName:'配置文件'},
    {typeCode:'source',type:'file',typeName:'源码文件'},
    {typeCode:'mAgent',type:'quota',typeName:'采集指标'},
    {typeCode:'sdk',type:'quota',typeName:'sdk指标'}
];


var draw;
$(function () {
    createDraw();  //创建拓扑图
    initPageEvent();  //绑定页面上的事件
    initDialog();   //初始化对话框.
    startMonitor(); //开始监控

});

//开即对拓扑图中节点和Link进行监控
var schemeData;
function startMonitor() {
    //每秒调用一次后台
    setInterval(function () {
        var schemeCode = $("#schemeCode").combobox("getValue");
        $.ajax({
            type: 'post',
            url: contextPath + '/console/scheme/getSchemeByCode',
            dataType: 'json',
            data: {
                schemeCode: schemeCode,
            },
            success: function (result) {
                if (result.code == 1){
                    //节点的状态
                    schemeData = result.object;
                    for (var i=0;i<schemeData.nodes.length;i++){
                        var node = schemeData.nodes[i];
                        var nodeCode = node.node;
                        var status = node.status;
                        var note = node.note;
                        draw.setNoteInfo(nodeCode,note,status);
                        if (status == "-1"){
                            draw.changeColor(nodeCode,"node",true,g_color_red);
                        }else if (status == "0"){
                            draw.changeColor(nodeCode,"node",false,g_color_yellow);
                        }else if (status == "1"){
                            draw.changeColor(nodeCode,"node",false,g_color_green);
                        }else if (status == "2"){
                            draw.changeColor(nodeCode,"node",false,g_color_blue);
                        }
                    }
                    //line的状态
                    for (var i=0;i<schemeData.lines.length;i++){
                        var line = schemeData.lines[i];
                        var lineCode = line.line;
                        var status = line.status;
                        var note = line.note;
                        draw.setLineInfo(lineCode,note);
                        if (status == "-1"){
                            draw.changeColor(lineCode,"line",true,g_color_red);
                        }else if (status == "0"){
                            draw.changeColor(lineCode,"line",false,g_color_yellow);
                        }else if (status  == "1"){
                            draw.changeColor(lineCode,"line",false,g_color_green);
                        }else if (status == "2"){
                            draw.changeColor(lineCode,"line",false,g_color_blue);
                        }
                    }
                }else{
                    apexLayer.Warn(result.note);
                }
            },
            error: function (xhr, status, error) {
                apexLayer.Warn(result.note);
            }
        });
    },1000);
}

//初始化任务列表
function initTaskTabl() {
    var nodeCode = $("#show_node_code").val();
    $("#taskTable").datagrid({
        width:550,
        height:'auto',
        url:contextPath+'/console/scheme/queryTaskList?nodeCode='+nodeCode,
        loadMsg: '加载中...',
        emptyMsg:'暂无数据',
        singleSelect:true,
        columns: [[
            {field:'taskName',title:'任务名称',width:'20%',align:'center'},
            {field:'taskCode',title:'任务代码',width:'16%',align:'center'},
            {field:'type',title:'任务类型',width:'20%',align:'center',
                formatter:function(value){
                    for(var i=0; i<taskTypeList.length; i++){
                        if (taskTypeList[i].typeCode == value){
                            return taskTypeList[i].typeName;
                        }
                    }
                    return value;
                },
            },
            {field:'value',title:'任务值',width:'40%',align:'center'}
        ]],
        init:function () {

        }
    });
}

//初始化对话框
function initDialog(){
    $('#nodeShow-dlg').dialog({
        width: 850,
        height: 500,
        closed: true,
        cache: false,
        modal: true,
        title:'中间件信息',
        buttons: [{
            text: '确认',
            iconCls: 'icon-save',
            handler: function () {
                $('#nodeShow-dlg').dialog('close');
            }
        }],
        onOpen: function (){
            initTaskTabl(); //初始化任务表格.
        },
        onClose: function () {
        }
    });
}
//创建拓扑图
function createDraw() {
    var packWidth = document.body.clientWidth;   //工作区宽度
    var packHeight = document.body.clientHeight -55; //工作区高度
    //面板属性
    var property={
        width:packWidth,
        height:packHeight,
        haveHead:false,
        toolBtns:["task round"],
        haveTool:false,
        haveGroup:false,
        useOperStack:false
    };
    //备注信息
    var remark={
        cursor:"选择指针",
        direct:"结点连线",
        "task":"结点",
    };
    //创建拓扑图
    draw=$.createDraw($("#draw"),property);
    //设置节点的说明
    draw.setNodeRemarks(remark);
}

//初始化页面按钮事件
function initPageEvent() {
    //音频测试
    $("#audioBtn").bind('click',function () {
        var warnAudio = '<embed src="'+contextPath+'/js/draw/audio_warning.wav" autostart="true" hidden="true" loop="false">';
        var errorAudio = '<embed src="'+contextPath+'/js/draw/audio_error.wav" autostart="true" hidden="true" loop="false">';
        $('body').append(errorAudio);
    });
    // //变颜色
    // $("#changeColorBtn").bind('click',function () {
    //     //黄色：#f1c40f   绿色：#27ae60 红色：#f14f0f
    //     var color = g_color_red;
    //     // draw.changeColor("draw_node_30","node",true,color);
    //     // draw.changeColor("draw_node_31","node",true,g_color_yellow);
    //     // draw.changeColor("draw_line_35","line",true,g_color_yellow);
    //     draw.changeColor("draw_line_34","line",true,g_color_green);
    //     //draw.changeColor("draw_line_39","line",true,g_color_red);
    //
    // });
    // $("#cleanBtn").bind('click',function () {
    //     //draw.clearData();
    //     draw.changeColor("draw_node_30","node",false,g_color_blue);
    //     draw.changeColor("draw_node_31","node",false,g_color_blue);
    //     draw.changeColor("draw_line_35","line",false,g_color_blue);
    //     draw.changeColor("draw_line_34","line",false,g_color_blue);
    //     draw.changeColor("draw_line_39","line",false,g_color_blue);
    // });

    //初始化下拉框
    getSchemeTitle($("#schemeCode"));
}


/**
 * 查询所有方案
 */
function getSchemeTitle(jqElement) {
    jqElement.combobox({
        method: 'get',
        url: contextPath + '/console/scheme/getAllScheme',
        valueField: 'schemeCode',
        textField: 'title',
        editable: false,
        panelHeight:　'auto',
        onLoadSuccess: function (data) {
            var cookieSchemeCode = $.cookie('schemePage_select');
            console.log(cookieSchemeCode);
            if (typeof cookieSchemeCode != "undefined"){
                for(var i =0 ;i<data.length;i++){
                    if(data[i].schemeCode==cookieSchemeCode){
                        jqElement.combobox('select', data[i].schemeCode);
                    }
                }
            }else{
                if (data.length > 0){
                    jqElement.combobox('select', data[0].schemeCode);
                }
            }

        },
        onLoadError: function () {

        },
        onShowPanel: function () {
            $(this).combobox('panel').height();
        },
        onChange: function (newValue,oldValue) {
            $.cookie('schemePage_select', newValue, { expires: 30 });
            //清空拓扑图数据,并加载新的数据
            draw.clearData();
            loadData(newValue);
        }
    });
}
//加载数据
function loadData(scheme) {
    draw.clearData();
    $.ajax({
        type: 'post',
        url: contextPath + '/console/scheme/getContent',
        dataType: 'json',
        data: {
            schemeCode: scheme
        },
        success: function (result) {
            if (result.code == 1){
                var dataObj = JSON.parse(result.object.content);
                draw.loadData(dataObj);
            }else{
                apexLayer.Warn(result.note);
            }
        },
        error: function (xhr, status, error) {
            apexLayer.Warn("调用服务失败");
        }
    });
}

